// 云存储账号管理API服务
// REF: 云存储账号管理-API文档@v1.0.0

import { apiClient, ApiResponse, PaginatedResponse } from '@/lib/api';

// 云存储账号类型定义
export interface CloudStorageAccount {
  id: number;
  userId?: number | null;  // 可为空，支持公共账号
  cloudProvider: string;
  cloudAccount: string;
  cloudPassword?: string;  // 密码（管理员可见）
  rootDirectoryId: string;
  storageQuota: number;
  isActive: boolean;
  isOnline?: boolean;      // 在线状态
  lastCheckAt?: string;    // 最后检测时间
  statusDisplay?: string;  // 状态显示文本
  cloudExpiresAt: string;
  createdAt: string;
  updatedAt: string;
}

// 创建云存储账号请求参数
export interface CreateCloudAccountRequest {
  cloudProvider: string;
  cloudAccount: string;
  cloudPassword: string;
  cloudToken?: string;
  cloudTokenInfo?: any;
  rootDirectoryId?: string;
  storageQuota?: number;
  isActive?: boolean;
  cloudExpiresAt?: string;
}

// 更新云存储账号请求参数
export interface UpdateCloudAccountRequest {
  cloudProvider?: string;   // 支持修改云存储提供商
  cloudAccount?: string;    // 支持修改云存储账号
  cloudPassword?: string;
  cloudToken?: string;
  cloudTokenInfo?: any;
  rootDirectoryId?: string;
  storageQuota?: number;
  isActive?: boolean;
  isOnline?: boolean;       // 支持手动设置在线状态
  cloudExpiresAt?: string;
}

// 云存储账号管理API
export const cloudAccountApi = {
  // 获取云存储账号列表
  getCloudAccounts: async (params?: {
    page?: number;
    page_size?: number;
    user_id?: number;
    cloud_provider?: string;
    is_active?: boolean;
    search?: string;
  }): Promise<PaginatedResponse<CloudStorageAccount>> => {
    const response = await apiClient.get('/admin/cloud-accounts', { params });
    return response.data;
  },

  // 获取单个云存储账号详情
  getCloudAccount: async (id: number): Promise<ApiResponse<CloudStorageAccount>> => {
    const response = await apiClient.get(`/admin/cloud-accounts/${id}`);
    return response.data;
  },

  // 创建云存储账号
  createCloudAccount: async (data: CreateCloudAccountRequest): Promise<ApiResponse<CloudStorageAccount>> => {
    const response = await apiClient.post('/admin/cloud-accounts', data);
    return response.data;
  },

  // 更新云存储账号
  updateCloudAccount: async (id: number, data: UpdateCloudAccountRequest): Promise<ApiResponse<CloudStorageAccount>> => {
    const response = await apiClient.put(`/admin/cloud-accounts/${id}`, data);
    return response.data;
  },

  // 删除云存储账号
  deleteCloudAccount: async (id: number): Promise<ApiResponse> => {
    const response = await apiClient.delete(`/admin/cloud-accounts/${id}`);
    return response.data;
  },

  // 检测云存储账号连接状态
  checkCloudAccount: async (id: number): Promise<ApiResponse<CloudStorageAccount>> => {
    const response = await apiClient.post(`/admin/cloud-accounts/${id}/check`);
    return response.data;
  },

  // 使用账号密码登录云存储账号
  loginCloudAccount: async (id: number): Promise<ApiResponse<CloudStorageAccount>> => {
    const response = await apiClient.post(`/admin/cloud-accounts/${id}/login`);
    return response.data;
  },

  // 获取用户的云存储账号列表
  getUserCloudAccounts: async (userId: number, params?: {
    page?: number;
    page_size?: number;
  }): Promise<PaginatedResponse<CloudStorageAccount>> => {
    const response = await apiClient.get(`/admin/users/${userId}/cloud-accounts`, { params });
    return response.data;
  }
};